/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package engine.entities.inventory;

import engine.entities.Player;
import engine.entities.inventory.container.ItemContainer;
import engine.entities.inventory.item.ContainerInventoryTable;
import engine.entities.inventory.item.FeetTable;
import engine.entities.inventory.item.HeadTable;
import engine.entities.inventory.item.LeftSideTable;
import engine.entities.inventory.item.PartyInventoryTable;
import engine.entities.inventory.item.RightSideTable;
import engine.game.Game;
import engine.game.hud.SpriteRenderer;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

/**
 *
 * @author NathanT
 */
public class InventoryRender extends JPanel {

    private Game game = Game.getGlobalGame();

    private PartyInventory partyInv;
    private ContainerInventory containerInv;
    private JPanel contentPanel;

    private PartyInventoryPanel partyPanel;
    private ContainerPanel containerPanel;

    public InventoryRender(PartyInventory partyInv, ContainerInventory containerInv) {
        this.partyInv = partyInv;
        this.containerInv = containerInv;

        setOpaque(false);

        GridBagConstraints c;
        this.setLayout(new GridBagLayout());

        contentPanel = new JPanel();
        contentPanel.setLayout(new GridBagLayout());
        contentPanel.setOpaque(false);

        partyPanel = new PartyInventoryPanel();
        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 0.0;
        c.weighty = 0.0;
        c.fill = GridBagConstraints.HORIZONTAL;
        contentPanel.add(partyPanel, c);

        c = new GridBagConstraints();
        c.gridx = 1;
        c.gridy = 0;
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.fill = GridBagConstraints.BOTH;
        contentPanel.add(Box.createRigidArea(new Dimension(10, 10)), c);

        containerPanel = new ContainerPanel();
        c = new GridBagConstraints();
        c.gridx = 2;
        c.gridy = 0;
        c.weightx = 0.0;
        c.weighty = 1.0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.SOUTH;
        contentPanel.add(containerPanel, c);

        // Centering the panel on the screen
        c = new GridBagConstraints();
        c.gridx = 1;
        c.gridy = 1;
        c.weightx = 1.0;
        c.weighty = 0.0;
        c.fill = GridBagConstraints.HORIZONTAL;
        this.add(contentPanel, c);

        c = new GridBagConstraints();
        c.gridx = 1;
        c.gridy = 0;
        c.weightx = 0.0;
        c.weighty = 0.0;
        c.fill = GridBagConstraints.BOTH;
        this.add(Box.createVerticalGlue(), c);

        c = new GridBagConstraints();
        c.gridx = 1;
        c.gridy = 2;
        c.weightx = 0.0;
        c.weighty = 0.0;
        c.fill = GridBagConstraints.BOTH;
        this.add(Box.createVerticalGlue(), c);

        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 1;
        c.weightx = 3.0;
        c.weighty = 0.0;
        c.fill = GridBagConstraints.BOTH;
        this.add(Box.createHorizontalGlue(), c);

        c = new GridBagConstraints();
        c.gridx = 2;
        c.gridy = 1;
        c.weightx = 3.0;
        c.weighty = 0.0;
        c.fill = GridBagConstraints.BOTH;
        this.add(Box.createHorizontalGlue(), c);

        containerPanel.setVisible(false);
    }

    public void setPlayer(int spot, Player player) {
        partyPanel.setPlayer(spot, player);
    }

    public void setContainer(ItemContainer container) {
        containerPanel.setContainer(container);
    }

    public void setShowContainer(boolean show) {
        containerPanel.setVisible(show);
    }
    
    private class PartyInventoryPanel extends JPanel {

        private Game game;

        private JTabbedPane partySwitcher;
        private PlayerInvPanel[] players = new PlayerInvPanel[4];

        public PartyInventoryPanel() {
            this.game = Game.getGlobalGame();
            init();
        }

        private void init() {
            partySwitcher = new JTabbedPane();
            players[0] = new PlayerInvPanel(game.getActivePlayer());
            partySwitcher.addTab(players[0].getPlayerName(), players[0]);
            partySwitcher.addTab("Member 2", new JPanel());
            partySwitcher.addTab("Member 3", new JPanel());
            partySwitcher.addTab("Member 4", new JPanel());

            this.setOpaque(false);
            this.setLayout(new GridBagLayout());
            GridBagConstraints c;

            c = new GridBagConstraints();
            c.gridx = 0;
            c.gridy = 0;
            c.weightx = 1.0;
            c.weighty = 0.0;
            c.fill = GridBagConstraints.HORIZONTAL;
            this.add(partySwitcher, c);

            c = new GridBagConstraints();
            c.gridx = 0;
            c.gridy = 1;
            c.weightx = 1.0;
            c.weighty = 0.0;
            c.fill = GridBagConstraints.HORIZONTAL;
            this.add(new PartyInventoryTable(partyInv), c);
        }

        public void setPlayer(int spot, Player player) {
            players[spot].setPlayer(player);
        }

        private class PlayerInvPanel extends JPanel {

            private final int PLAYER_ICON_SCALE = 3;

            private Player player;
            private SpriteRenderer playerLabel;

            private JPanel playerPanel;
            private LeftSideTable leftTable;
            private RightSideTable rightTable;
            private HeadTable headTable;
            private FeetTable feetTable;
            
            private JButton closeButton;

            public PlayerInvPanel(Player player) {
                this.player = player;

                playerLabel = new SpriteRenderer(player.getStaticSprite(), PLAYER_ICON_SCALE);

                this.setLayout(new GridBagLayout());
                GridBagConstraints c;
                
                // Shameless hack, but it's the only way to get rid of the default padding for
                //   JTabbedPane
                this.setBorder(BorderFactory.createEmptyBorder(-10, 0, -10, 0));

                playerPanel = new JPanel();
                playerPanel.setLayout(new GridBagLayout());
                leftTable = new LeftSideTable(player);
                c = new GridBagConstraints();
                c.gridx = 0;
                c.gridy = 1;
                c.weightx = 0.0;
                c.weighty = 0.0;
                c.fill = GridBagConstraints.BOTH;
                playerPanel.add(leftTable, c);

                c = new GridBagConstraints();
                c.gridx = 1;
                c.gridy = 1;
                c.weightx = 0.0;
                c.weighty = 0.0;
                c.fill = GridBagConstraints.NONE;
                playerPanel.add(playerLabel, c);

                rightTable = new RightSideTable(player);
                c = new GridBagConstraints();
                c.gridx = 2;
                c.gridy = 1;
                c.weightx = 0.0;
                c.weighty = 0.0;
                c.fill = GridBagConstraints.BOTH;
                playerPanel.add(rightTable, c);

                headTable = new HeadTable(player);
                c = new GridBagConstraints();
                c.gridx = 1;
                c.gridy = 0;
                c.weightx = 0.0;
                c.weighty = 0.0;
                c.fill = GridBagConstraints.NONE;
                playerPanel.add(headTable, c);

                feetTable = new FeetTable(player);
                c = new GridBagConstraints();
                c.gridx = 1;
                c.gridy = 2;
                c.weightx = 0.0;
                c.weighty = 0.0;
                c.fill = GridBagConstraints.NONE;
                playerPanel.add(feetTable, c);
                
                // Adding this before the playerPanel puts it on top, and allows the playerPanel to still be centered
                closeButton = new JButton("X");
                closeButton.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        game.setInventory(false);
                    }

                });
                c = new GridBagConstraints();
                c.gridx = 1;
                c.gridy = 0;
                c.weightx = 0;
                c.weighty = 0.0;
                c.fill = GridBagConstraints.NONE;
                c.anchor = GridBagConstraints.NORTHEAST;
                this.add(closeButton, c);
                
                c = new GridBagConstraints();
                c.gridx = 0;
                c.gridy = 0;
                c.weightx = 1.0;
                c.weighty = 0.0;
                c.gridwidth = 2;
                c.fill = GridBagConstraints.HORIZONTAL;
                this.add(playerPanel, c);

            }

            public String getPlayerName() {
                return player.getName();
            }

            public void setPlayer(Player player) {
                leftTable.setPlayer(player);
                rightTable.setPlayer(player);
                headTable.setPlayer(player);
                feetTable.setPlayer(player);
            }
        }

    }

    private class ContainerPanel extends JPanel {

        private ContainerInventoryTable invTable;

        public ContainerPanel() {
            this.setLayout(new GridBagLayout());
            GridBagConstraints c;

            invTable = new ContainerInventoryTable(containerInv);
            c = new GridBagConstraints();
            c.gridx = 0;
            c.gridy = 2;
            c.weightx = 0.0;
            c.weighty = 0.0;
            c.fill = GridBagConstraints.NONE;
            this.add(invTable, c);
        }

        public void setContainer(ItemContainer container) {
            invTable.setInventory(container.getInventory());
        }

    }

}
